package br.edu.fcsl.projetoIntegrador.Frame;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import br.edu.fcsl.projetoIntegrador.ClassFile.Acesso;
import br.edu.fcsl.projetoIntegrador.ClassFile.LeituraDoArquivo;
import br.edu.fcsl.projetoIntegrador.ClassFile.ListaDeAcesso;
import br.edu.fcsl.projetoIntegrador.ClassFile.Log;
import br.edu.fcsl.projetoIntegrador.ClassFile.ReportChart;

public class JFrame_Principal extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JLabel lblAbrir;
	private JButton btnNewButton_2;
	private JTextField textField;
	private String caminhoArquivo;
	/**
	 * @wbp.nonvisual location=474,49
	 */
	private final JFileChooser fileChooser = new JFileChooser();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame_Principal frame = new JFrame_Principal();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrame_Principal() {
		setIconImage(Toolkit
				.getDefaultToolkit()
				.getImage(
						JFrame_Principal.class
								.getResource("/br/edu/fcsl/projetoIntegrador/Img/Log Report.png")));
		setTitle("Log Report");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		btnNewButton = new JButton("Cancelar");
		btnNewButton.addActionListener(this);

		btnNewButton_1 = new JButton("Avan\u00E7ar");
		btnNewButton_1.addActionListener(this);

		lblAbrir = new JLabel("Arquivo :");

		btnNewButton_2 = new JButton("Abrir");
		btnNewButton_2.addActionListener(this);

		textField = new JTextField();
		textField.setColumns(10);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																Alignment.TRAILING,
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				btnNewButton_1)
																		.addGap(18)
																		.addComponent(
																				btnNewButton))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				lblAbrir)
																		.addGap(18)
																		.addComponent(
																				textField,
																				GroupLayout.DEFAULT_SIZE,
																				269,
																				Short.MAX_VALUE)
																		.addGap(18)
																		.addComponent(
																				btnNewButton_2)))
										.addContainerGap()));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								Alignment.TRAILING,
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap(185, Short.MAX_VALUE)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lblAbrir)
														.addComponent(
																btnNewButton_2)
														.addComponent(
																textField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																btnNewButton)
														.addComponent(
																btnNewButton_1))
										.addContainerGap()));
		contentPane.setLayout(gl_contentPane);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnNewButton) {
			do_btnNewButton_actionPerformed(arg0);
		}
		if (arg0.getSource() == btnNewButton_2) {
			try {
				do_btnNewButton_2_actionPerformed(arg0);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (arg0.getSource() == btnNewButton_1) {
			do_btnNewButton_1_actionPerformed(arg0);
		}
	}

	protected void do_btnNewButton_1_actionPerformed(ActionEvent arg0) {

		LeituraDoArquivo leituraDoArquivo = new LeituraDoArquivo();

		ListaDeAcesso listaDeAcesso = new ListaDeAcesso();

		JFrame_Grafico jfreFrame_Grafico;

		List<Log> listaLog;

		List<Acesso> listaAcesso;

		listaLog = leituraDoArquivo.lerLog(caminhoArquivo);

		listaAcesso = listaDeAcesso.maisAcessados(listaLog);

		ReportChart reportChart = new ReportChart(listaAcesso);

		JFreeChart grafico = reportChart.grafico();

		this.add(new ChartPanel(grafico));

		JFrame_Grafico jFrameGrafico = new JFrame_Grafico();
		jFrameGrafico.add(new ChartPanel(grafico));
		jFrameGrafico.setVisible(true);
	}

	protected void do_btnNewButton_2_actionPerformed(ActionEvent arg0)
			throws FileNotFoundException {

		int retorno = fileChooser.showSaveDialog(null);

		if (retorno == JFileChooser.APPROVE_OPTION) {
			caminhoArquivo = fileChooser.getSelectedFile().getAbsolutePath();
			textField.setText(this.caminhoArquivo);
		} else {

			retorno = JFileChooser.CANCEL_OPTION;

		}
	}

	protected void do_btnNewButton_actionPerformed(ActionEvent arg0) {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}
